package com.seaengine.soundmanager
{
	
	import as3.component.sound.GlobalSoundProperty;
	import as3.component.sound.SceneSoundProperty;
	import as3.component.sound.SoundOverride;
	import as3.component.sound.UISoundProperty;
	
	import com.seaengine.GameConfig;
	import com.seaengine.constData.SoundType;
	import com.seaengine.resourcemanager.loadtool.ILoader;
	import com.seaengine.resourcemanager.loadtool.LoadUtils;
	import com.seaengine.util.Cookie;
	import com.seaengine.valueobject.SoundVO;
	
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	

	/**
	 * 声音管理类
	 * @author 曹鹏
	 * 二〇一一年十月十七日 17:45:17
	 */	
	public class SoundManager
	{
	
		//--------------------------------------------------------------------------
		//
		//  variable
		//
		//--------------------------------------------------------------------------
		private static const URL:String = "sound/";
		
		private static var instance:SoundManager;
		
		/**音效路径*/
		private var soundLibrary:Dictionary;
		
		/**音效对象*/
		private var soundDictionary:Dictionary;
		
		/**场景音效播放列表*/
		private var sceneSoundList:Array;
		
		private var url:URLRequest;
		
		private var loadUtils:LoadUtils;
		
		public function SoundManager()
		{
			init();
		}
		//--------------------------------------------------------------------------
		//
		//  property
		//
		//--------------------------------------------------------------------------
		private var _globalSound:GlobalSoundProperty;

		/**全局声音属性*/
		public function get globalSound():GlobalSoundProperty
		{
			return _globalSound;
		}

		/**
		 * @private
		 */
		public function set globalSound(value:GlobalSoundProperty):void
		{
			_globalSound = value;
		}
		
		private var _uiSound:UISoundProperty;

		/**UI声音属性*/
		public function get uiSound():UISoundProperty
		{
			return _uiSound;
		}

		/**
		 * @private
		 */
		public function set uiSound(value:UISoundProperty):void
		{
			_uiSound = value;
		}

		
		private var _sceneSound:SceneSoundProperty;

		/**场景声音属性*/
		public function get sceneSound():SceneSoundProperty
		{
			return _sceneSound;
		}

		/**
		 * @private
		 */
		public function set sceneSound(value:SceneSoundProperty):void
		{
			_sceneSound = value;
		}

		//--------------------------------------------------------------------------
		//
		//  static
		//
		//--------------------------------------------------------------------------
		public static function getInstance():SoundManager
		{
			if(instance == null)
			{
				instance = new SoundManager();
			}
			return instance;
		}
		//--------------------------------------------------------------------------
		//
		//  public
		//
		//--------------------------------------------------------------------------
		/**
		 * 获得声音
		 * @param id 声音ID
		 */		
		public function getSound(id:String):Sound
		{
			return soundDictionary[id];
		}
		
		/**
		 * 播放声音
		 * @param id 声音ID
		 */		
		public function play(id:String):void
		{
			if(id == "" || id == null)
			{
				return;
			}
			var sound:SoundOverride = soundDictionary[id];
			if(sound != null)
			{
				if(sound.isPlay == true)
				{
					sound.stop();
				}
				sound.play();
			}
			else
			{
				load(id);
			}
		}
		
		/**
		 * 停止声音
		 * @param id 声音ID
		 */	
		public function stop(id:String):void
		{
			var sound:SoundOverride = soundDictionary[id];
			if(sound != null)
			{
				sound.stop();
			}
		}
		
		/**
		 * 静音
		 */		
		public function mute():void
		{
			globalSound.mute();
		}
		
		/**
		 * 取消静音
		 */		
		public function cancelMute():void
		{
			globalSound.cancelMute();
		}
		
		/**
		 * 停止播放当前所有声音
		 */		
		public function stopAll():void
		{
			SoundMixer.stopAll();
		}
		
		/**
		 * 开启背景，战斗音乐
		 */		
		public function playSceneSound():void
		{
			sceneSound.isMute = false;
			sceneSound.volume = 1;
		}
		
		/**
		 * 关闭背景，战斗音乐
		 */		
		public function stopSceneSound():void
		{
			sceneSound.isMute = false;
			sceneSound.volume = 0;
			for each(var sound:SoundOverride in sceneSoundList)
			{
				sound.stop();
			}
		}
		
		/**
		 * 开启UI音效
		 */		
		public function playUISound():void
		{
			uiSound.isMute = false;
			uiSound.volume = 1;
		}
		
		/**
		 * 关闭UI音效
		 */		
		public function stopUISound():void
		{
			uiSound.isMute = true;
			uiSound.volume = 0;
		}
		
		//--------------------------------------------------------------------------
		//
		//  private
		//
		//--------------------------------------------------------------------------
		private function init():void
		{
			soundLibrary = new Dictionary();
			soundDictionary = new Dictionary();
			sceneSoundList = new Array();
			loadUtils = new LoadUtils();
			sceneSound = new SceneSoundProperty();
			uiSound = new UISoundProperty();
			globalSound = new GlobalSoundProperty();
			url = new URLRequest();
			loadUtils.addXMLURL(URL + "sound.xml",soundXMLComplete);
			loadUtils.load();
		}
		
		private function load(id:String):void
		{
			var vo:SoundVO = soundLibrary[id];
			if(vo != null)
			{
				var sound:SoundOverride = new SoundOverride();
				sound.vo = vo;
				sound.id = id;
				sound.load(GameConfig.m_ResConfig + vo.url,completeCallFunction);
			}
		}

		//--------------------------------------------------------------------------
		//
		//  event
		//
		//--------------------------------------------------------------------------
		private function soundXMLComplete(iLoader:ILoader):void
		{
			var xml:XML = XML(iLoader.data);
			var xmlList:XMLList = xml.soundLibrary;
			for(var a:uint = 0; a < xmlList.length(); a++)
			{
				var name:String = xmlList[a].@name;
				var id:String = xmlList[a].@id;
				var fileName:String = xmlList[a].@fileName;
				var type:uint = xmlList[a].@type;
				var vo:SoundVO = new SoundVO;
				vo.name = name;
				vo.id = id;
				vo.type = type;
				vo.url = URL + fileName;
				soundLibrary[id] = vo;
			}
		}
		
		private function completeCallFunction(sound:SoundOverride):void
		{
			if(sound != null)
			{
				soundDictionary[sound.id] = sound;
				if(sound.vo.type == SoundType.SCENE)
				{
					sceneSoundList.push(sound);
				}
			}
		}
	}
}